﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Org.BouncyCastle.Crypto.Prng;

namespace PropertyManagementSystemAPI.Controllers
{
    /// <summary>
    /// 投诉管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class ComplaintInfoController : ControllerBase
    {
        IFreeSql db;
        public ComplaintInfoController(IFreeSql _db)
        {
            db = _db;
        }

        /// <summary>
        /// 查询全部数据
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="limit">条数</param>
        /// <param name="cremark">车牌号</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInfo(int page, int limit, string? cremark)
        {
            var list = db.Select<Models.Complaintinfo, Models.Ownerinfo>()
                .LeftJoin((a, b) => a.Oid == b.Oid)
                .Where((a, b) => ((string.IsNullOrEmpty(cremark)) || (a.Cremark.Contains(cremark))) && (a.Isdelete == false))
                .Count(out var total)
                .Page(page, limit)
                .ToList((a, b) => new
                {
                    a.Complaintid,
                    a.Ctitle,
                    a.Cremark,
                    a.Createtime,
                    a.State,
                    b.Oid,
                    b.Orealname
                });
            var json = new
            {
                code = 200,
                data = list,
                total = total,
            };
            return Ok(json);
        }

        /// <summary>
        /// 添加投诉信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Add([FromBody] Models.Complaintinfo info)
        {
            try
            {
                int list = db.Insert(info).ExecuteAffrows();
                return Ok(list);
            }
            catch (Exception ex)
            {
                return Ok(ex);
            }
        }

        /// <summary>
        /// 修改投诉信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult Update([FromBody] Models.Complaintinfo info)
        {
            try
            {
                info.Isdelete = false;
                int list = db.Update<Models.Complaintinfo>().SetSource(info).ExecuteAffrows();
                return Ok(list);
            }
            catch (Exception ex)
            {
                return Ok(false);
            }
        }

        /// <summary>
        /// 删除投诉信息
        /// </summary>
        /// <param name="complaintid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult Del(int? complaintid)
        {
            try
            {
                int list = db.Update<Models.Complaintinfo>(complaintid)
                    .Set(x => x.Isdelete == true)
                    .ExecuteAffrows();
                return Ok(list > 0);
            }
            catch (Exception ex)
            {
                return Ok(false);
            }
        }
    }
}
